package com.shao.demo.feignconsumer.web;

import com.shao.demo.feignconsumer.common.BaseRuntimeException;
import com.shao.demo.feignconsumer.common.base.BaseController;
import com.shao.demo.feignconsumer.common.constans.StatusCode;
import com.shao.demo.feignconsumer.rpc.HelloService;
import com.shao.demo.feignconsumer.common.wapper.ResponseEntityWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhiqi.shao
 * @Date 2018/5/23 16:29
 */
@Slf4j
@RestController
public class FeignController extends BaseController {

    @Autowired
    private HelloService helloService;

    @GetMapping(value = "/feign/hello")
    public ResponseEntityWrapper sayHello(@RequestParam String name, HttpServletRequest request) {
        log.info("我是feign---------------name:{}", name);
        log.info("trace Id:{} spanId:{}",request.getHeader("X-B3-Traceid"),request.getHeader("X-B3-Spanid"));
        //下面这三行代码应该写在业务层中，此处做测试所以搬到了controller
        ResponseEntityWrapper resultWrapper = helloService.sayHello(name);
        if(resultWrapper.getStatusCode()!= StatusCode.SUCCESS.getStatusCode()){
           throw new BaseRuntimeException(resultWrapper.getStatusCode(),resultWrapper.getStatusMessage());
        }
        return success(resultWrapper.getData());
    }
}
